package org.jsola.hr.helper;

import org.jsola.common.DateKit;
import org.jsola.common.SafeKit;
import org.jsola.hr.constant.ProgTypeEnum;
import org.jsola.hr.dao.IAttendanceDedDAO;
import org.jsola.hr.dao.IAttendanceDedItemDAO;
import org.jsola.hr.dto.*;
import org.jsola.hr.entity.AttendanceDedDO;
import org.jsola.hr.entity.AttendanceDedItemDO;
import org.jsola.hr.entity.AttendanceDedParamDO;
import org.jsola.hr.entity.AttendanceDedProgDO;
import org.jsola.hr.query.AttendanceDedProgQuery;
import org.jsola.hr.service.IAttendanceDedProgService;
import org.jsola.hr.service.IAttendanceDedService;
import org.jsola.hr.vo.AttendanceDedProgVO;
import org.jsola.hr.vo.EmpInfoVO;
import org.jsola.module.ModuleContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import static org.jsola.hr.core.AbstractTest.SITE_ID;
import static org.jsola.hr.core.TestCommonConstans.ATTENDANCE_DED_PROG_DO_LIST;
import static org.jsola.hr.core.TestCommonConstans.CODE_EMP_LIST;


/**
 * @author zhr
 */
@Component
public class AttendanceDedProgTestHelper extends BaseTestHelper {
    
    @Autowired
    private IAttendanceDedProgService attendanceDedProgService;

    @Autowired
    private IAttendanceDedService attendanceDedService;

    @Autowired
    private IAttendanceDedDAO attendanceDedDAO;

    @Autowired
    private IAttendanceDedItemDAO attendanceDedItemDAO;

    /**
     * 生成add测试数据
     * @return AttendanceDedProgAddDTO
     */
    public AttendanceDedProgAddDTO getAttendanceDedProgAddDTO(){
        AttendanceDedProgAddDTO attendanceDedProgAddDTO = new AttendanceDedProgAddDTO();
        attendanceDedProgAddDTO.setName(getRandomValue("varchar",191));
        attendanceDedProgAddDTO.setRemark(getRandomValue("varchar",255));
        attendanceDedProgAddDTO.setCompanyId(getRandomValue("varchar",32));
        return attendanceDedProgAddDTO;
    }


    /**
     * 生成update测试数据
     * @return AttendanceDedProgUpdateDTO
     */
    public AttendanceDedProgUpdateDTO getAttendanceDedProgUpdateDTO(AttendanceDedProgVO attendanceDedProgVO){
        return attendanceDedProgService.selectDOById(attendanceDedProgVO.getId(), SITE_ID).to(AttendanceDedProgUpdateDTO.class);
    }

    /**
     * 生成查询数据
     * @return AttendanceDedProgQuery
     */
    public AttendanceDedProgQuery getAttendanceDedProgQuery() {
        AttendanceDedProgQuery attendanceDedProgQuery = new AttendanceDedProgQuery();
        List<AttendanceDedProgDO> attendanceDedProgDOList = attendanceDedProgService.list(new AttendanceDedProgDO(),SITE_ID);
        if (!CollectionUtils.isEmpty(attendanceDedProgDOList)) {
            AttendanceDedProgDO attendanceDedProgDO = attendanceDedProgDOList.get(0);
            attendanceDedProgQuery = attendanceDedProgDO.to(AttendanceDedProgQuery.class);
            if (attendanceDedProgDO.getGmtCreate() != null) {
                attendanceDedProgQuery.setGmtCreateStart(DateKit.getBeforeDay(attendanceDedProgDO.getGmtCreate(),-1));
                attendanceDedProgQuery.setGmtCreateEnd(DateKit.getBeforeDay(attendanceDedProgDO.getGmtCreate(),1));
            }
            if (attendanceDedProgDO.getGmtModified() != null) {
                attendanceDedProgQuery.setGmtModifiedStart(DateKit.getBeforeDay(attendanceDedProgDO.getGmtModified(),-1));
                attendanceDedProgQuery.setGmtModifiedEnd(DateKit.getBeforeDay(attendanceDedProgDO.getGmtModified(),1));
            }
        }
        return attendanceDedProgQuery;
    }

    /**
     * 保存示例
     * @return AttendanceDedProgVO
     */
    public AttendanceDedProgVO saveAttendanceDedProgExample(){
        AttendanceDedProgAddDTO addDTO = this.getAttendanceDedProgAddDTO();
        addDTO.setApplyToProgAddDTO(new ApplyToProgAddDTO());
        AttendanceDedProgVO attendanceDedProgVO = attendanceDedProgService.save(addDTO,buildTokenUser());
        return attendanceDedProgVO;
    }

    /**
     * 批量生成add测试数据
     * @return moduleContext
     */
    public ModuleContext batchSaveAttendanceDedProgExample(ModuleContext moduleContext){
        List<EmpInfoVO> empInfoVOList = moduleContext.get(CODE_EMP_LIST);
//        for (int i = 0; i < 10; i++){
            AttendanceDedProgAddDTO attendanceDedProgAddDTO = new AttendanceDedProgAddDTO();
            attendanceDedProgAddDTO.setName(getRandomValue("varchar",191));
            attendanceDedProgAddDTO.setRemark(getRandomValue("varchar",255));
            attendanceDedProgAddDTO.setCompanyId(empInfoVOList.get(0).getCompanyId());

            //添加考勤扣款设置列表
            List<AttendanceDedDO> attendanceDedDOList = new ArrayList<>();
            for(int j = 0; j < 5; j++){
                AttendanceDedAddDTO attendanceDedAddDTO = new AttendanceDedAddDTO();
                attendanceDedAddDTO.setProgrammeId("");
                attendanceDedAddDTO.setDedType(SafeKit.getString(j + 1));
//                attendanceDedAddDTO.setDedType(SafeKit.getString(getRandomValueByRange(5, 0)));
                attendanceDedAddDTO.setName(getRandomValue("varchar",255));
                attendanceDedAddDTO.setType(SafeKit.getInteger(getRandomValueByRange(2, 1)));
                attendanceDedAddDTO.setAmountMinute(SafeKit.getInteger(getRandomValueByRange(50, 20)));
                attendanceDedAddDTO.setAmountAbsenteeism(SafeKit.getInteger(getRandomValueByRange(3, 1)));
                attendanceDedAddDTO.setOverallRange("lateTimes,leaveTimes,absenteeismTimes,supplementTimes");
                attendanceDedAddDTO.setStatus(SafeKit.getBoolean(getRandomValueByRange(1, 0) == 0 ? false : true));
                attendanceDedAddDTO.setCompanyId(empInfoVOList.get(0).getCompanyId());
                AttendanceDedDO attendanceDedDO = attendanceDedAddDTO.to(AttendanceDedDO.class);

                //添加考勤扣款设置子项列表
                List<AttendanceDedItemDO> attendanceDedItemDOList = new ArrayList<>();
                for(int k = 0; k < 5; k++){
                    AttendanceDedItemAddDTO attendanceDedItemAddDTO = new AttendanceDedItemAddDTO();
                    attendanceDedItemAddDTO.setTimeOperator(getRandomValueByRange(2, 1));
                    attendanceDedItemAddDTO.setTimeValue(getRandomValueByRange(60, 10));
                    attendanceDedItemAddDTO.setCompanyId(empInfoVOList.get(0).getCompanyId());
                    attendanceDedItemAddDTO.setParentId("");
                    attendanceDedItemAddDTO.setSort(getRandomValueByRange(999, 1));
                    attendanceDedItemAddDTO.setLevel(getRandomValueByRange(5, 1));
                    AttendanceDedItemDO attendanceDedItemDO = attendanceDedItemAddDTO.to(AttendanceDedItemDO.class);

                    //添加考勤扣款设置参数列表
                    List<AttendanceDedParamDO> attendanceDedParamDOList = new ArrayList<>();
                    for(int l = 0; l < 5; l++){
                        AttendanceDedParamAddDTO attendanceDedParamAddDTO = new AttendanceDedParamAddDTO();
                        attendanceDedParamAddDTO.setItemId("");
                        attendanceDedParamAddDTO.setTimesOperator(getRandomValueByRange(2, 1));
                        attendanceDedParamAddDTO.setTimesValue(getRandomValueByRange(10, 1));
                        attendanceDedParamAddDTO.setDedType(getRandomValueByRange(2, 1));
                        attendanceDedParamAddDTO.setDedAmount(getRandomValueByRange(200, 50));
                        attendanceDedParamAddDTO.setAbsenteeismType(1);
                        attendanceDedParamAddDTO.setAbsenteeismValue(SafeKit.getBigDecimal(getRandomValueByRange(3, 1)));
                        attendanceDedParamAddDTO.setCompanyId(empInfoVOList.get(0).getCompanyId());
                        attendanceDedParamDOList.add(attendanceDedParamAddDTO.to(AttendanceDedParamDO.class));
                    }

                    attendanceDedItemDO.setAttendanceDedParamDOList(attendanceDedParamDOList);
                    attendanceDedItemDOList.add(attendanceDedItemDO);


                }
                attendanceDedDO.setAttendanceDedItemDOList(attendanceDedItemDOList);
                attendanceDedDOList.add(attendanceDedDO);
            }

            attendanceDedProgAddDTO.setAttendanceDedDOList(attendanceDedDOList);

            ApplyToProgAddDTO applyToProgAddDTO = new ApplyToProgAddDTO();
//            if(i == 0){
                applyToProgAddDTO.setBusProgType(ProgTypeEnum.ATTENDANCE_DED.getValue());
                applyToProgAddDTO.setEmployRange("1");
                applyToProgAddDTO.setMutex(true);
                applyToProgAddDTO.setCompanyId(empInfoVOList.get(0).getCompanyId());

                List<ApplyToProgDepAddDTO> applyToProgDepAddDTOList = new ArrayList<>();
                for (EmpInfoVO empInfoVO : empInfoVOList) {
                    ApplyToProgDepAddDTO applyToProgDepAddDTO = new ApplyToProgDepAddDTO();
                    applyToProgDepAddDTO.setType(2);
                    applyToProgDepAddDTO.setDepId(empInfoVO.getId());
                    applyToProgDepAddDTO.setCompanyId(empInfoVOList.get(0).getCompanyId());
                    applyToProgDepAddDTO.setProgId("");
                    applyToProgDepAddDTOList.add(applyToProgDepAddDTO);
                }

                applyToProgAddDTO.setApplyToProgDepAddDTOList(applyToProgDepAddDTOList);
//            }else{
//                applyToProgAddDTO.setBusProgType(ProgTypeEnum.ATTENDANCE_DED.getValue());
//                applyToProgAddDTO.setEmployType(SafeKit.getString(i + 1));
//                applyToProgAddDTO.setEmployRange(SafeKit.getString(i + 1));
//                applyToProgAddDTO.setMutex(true);
//                applyToProgAddDTO.setCompanyId(empInfoVOList.get(0).getCompanyId());
//
//                List<ApplyToProgDepAddDTO> applyToProgDepAddDTOList = new ArrayList<>();
//                ApplyToProgDepAddDTO applyToProgDepAddDTO = new ApplyToProgDepAddDTO();
//                applyToProgDepAddDTO.setType(2);
//                applyToProgDepAddDTO.setDepId(SafeKit.getString(i + 1));
//                applyToProgDepAddDTO.setCompanyId(empInfoVOList.get(0).getCompanyId());
//                applyToProgDepAddDTO.setProgId("");
//                applyToProgDepAddDTOList.add(applyToProgDepAddDTO);
//
//                List<ApplyToProgPositionAddDTO> applyToProgPositionAddDTOList = new ArrayList<>();
//                ApplyToProgPositionAddDTO applyToProgPositionAddDTO = new ApplyToProgPositionAddDTO();
//                applyToProgPositionAddDTO.setPositionId(SafeKit.getString(i + 1));
//                applyToProgPositionAddDTO.setCompanyId(empInfoVOList.get(0).getCompanyId());
//                applyToProgPositionAddDTO.setProgId("");
//                applyToProgPositionAddDTOList.add(applyToProgPositionAddDTO);
//
//                applyToProgAddDTO.setApplyToProgDepAddDTOList(applyToProgDepAddDTOList);
//                applyToProgAddDTO.setApplyToProgPositionAddDTOList(applyToProgPositionAddDTOList);
//                applyToProgAddDTO.setBusProgType(ProgTypeEnum.ATTENDANCE_DED.getValue());
//                applyToProgAddDTO.setEmployRange("1");
//                applyToProgAddDTO.setMutex(true);
//                applyToProgAddDTO.setCompanyId(empInfoVOList.get(0).getCompanyId());
//            }

            attendanceDedProgAddDTO.setApplyToProgAddDTO(applyToProgAddDTO);
            AttendanceDedProgVO attendanceDedProgVO = attendanceDedProgService.save(attendanceDedProgAddDTO,buildTokenUser());
//        }

        //查询扣款方案列表
        AttendanceDedProgDO attendanceDedProgDOTmp = new AttendanceDedProgDO();
        attendanceDedProgDOTmp.setValid(true);
        attendanceDedProgDOTmp.setSiteId(buildTokenUser().getSiteId());
        attendanceDedProgDOTmp.setCompanyId(empInfoVOList.get(0).getCompanyId());
        List<AttendanceDedProgDO> attendanceDedProgDOList = attendanceDedService.progListWithItem(attendanceDedProgDOTmp, buildTokenUser().getSiteId());

        moduleContext.add(ATTENDANCE_DED_PROG_DO_LIST, attendanceDedProgDOList);
        return moduleContext;
    }

    public Integer getRandomValueByRange(int max, int min){
        int num = new Random().nextInt(max - min + 1) + min;
//        System.out.println(num);
        return num;
    }

//    public static void main(String[] args){
//        int max = 3;
//        int min = 1;
//        for(int i = 0; i < 30; i++){
//            int num = new Random().nextInt(max - min + 1) + min;
//            System.out.println(num);
//        }
//    }
}

